/* eslint-disable react-compiler/react-compiler */
import { useState, useMemo } from "react";
/* function _c(size) {
    //创建一个长度为size的缓存数组
    let array = new Array(size)
    //向每一项填充默认值
    array.fill(Symbol.for("react.memo_cache_sentinel"));
    return array;
} */

/* function Counter() {
    const [count, setCount] = useState(0);
    const handleClick = () => {
        setCount(count + 1)
    }
    return <button onClick={handleClick}>{count}</button>
} */
function useCache() {
  const cache = useMemo((size) => {
    let array = new Array(size);
    array.fill(Symbol.for("react.memo_cache_sentinel"));
    return array;
  }, []);
  return cache;
}
function Counter() {
  //初始化缓存数组，支持最多5个缓存位置
  const cache = useCache(5);
  console.log(cache);
  //使用useState创建状态和更新函数
  const [count, setCount] = useState(0);
  //缓存点击处理函数
  let handleClick;
  //如果当前的count值和cache[0]不一样，可能有两种情况，

  //第一次渲染的时候Symbol.for("react.memo_cache_sentinel")
  //后面更新的时候可能是新的count值
  if (cache[0] !== count) {
    //如果缓存中的计数值与当前的计不一致，更新缓存
    handleClick = () => setCount(count + 1);
    cache[0] = count; //缓存当前的计数值
    cache[1] = handleClick; //缓存生成的处理函数
  } else {
    //如果缓存一致，则直接用上一个处理函数
    handleClick = cache[1];
  }
  //缓存按钮JSX元素
  let buttonElement;
  if (cache[2] !== count || cache[3] !== handleClick) {
    //如果计数值或处理函数发生变化，更新缓存
    buttonElement = <button onClick={handleClick}>{count}</button>;
    cache[2] = count; //缓存当前的计数值
    cache[3] = handleClick; //缓存当前处理点击函数
    cache[4] = buttonElement; //缓存生成的按钮元素
  } else {
    //如果缓存一致，则直接复用按钮的元素
    buttonElement = cache[4];
  }
  return buttonElement;
}
export default Counter;

/**
function Counter() {
  const $ = _c(5);
  const [count, setCount] = useState(0);
  let t0;
  if ($[0] !== count) {
    t0 = () => {
      setCount(count + 1);
    };
    $[0] = count;
    $[1] = t0;
  } else {
    t0 = $[1];
  }
  const handleClick = t0;
  let t1;
  if ($[2] !== count || $[3] !== handleClick) {
    t1 = <button onClick={handleClick}>{count}</button>;
    $[2] = count;
    $[3] = handleClick;
    $[4] = t1;
  } else {
    t1 = $[4];
  }
  return t1;
}

 */
